{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import lightgbm as lgb\n",
    "from datetime import datetime\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import StratifiedKFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_path = '../../../contest/train/'\n",
    "stage_path = '../../../contest/B榜/'\n",
    "stage = 'B'\n",
    "end_date_train = datetime(1993,11,29)\n",
    "end_date_test = datetime(1994,1,30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.read_csv('../../../contest/train/DZ_TARGET_TRAIN.csv')\n",
    "df_test = pd.read_csv('../../../contest/B榜/DZ_TARGET_TESTB.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train_user = pd.DataFrame({'CUST_NO':df_train.CUST_NO})\n",
    "df_test_user = pd.DataFrame({'CUST_NO':df_test.cust_no})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def normal_stats(data, group_col, agg_col, fea_prefix, func_trans = ['sum','mean','size', 'max', 'min', 'std' ] ):\n",
    "    grp_data = data.groupby(group_col)[agg_col].agg(\n",
    "        func_trans\n",
    "    )\n",
    "    grp_data.columns = [ f'{fea_prefix}_{group_col}_{agg_col}_{x}' for x in func_trans ]\n",
    "    grp_data = grp_data.reset_index()\n",
    "    return grp_data\n",
    "    \n",
    "\n",
    "def slide_window_stats(data, time_col, group_col,agg_col, window_size, fea_prefix, end_date,func_trans = ['sum','mean', 'count', 'max', 'min', 'std' ]):\n",
    "    #func_trans = ['sum','mean', 'size', 'max', 'min', 'std' ]\n",
    "    data['date_gap'] = (end_date - data[time_col]).dt.days + 1\n",
    "    sub_data = data[data['date_gap']<= window_size]\n",
    "    grp_data = sub_data.groupby(group_col)[agg_col].agg(\n",
    "        func_trans\n",
    "    )\n",
    "    grp_data.columns = [ f'{fea_prefix}_{group_col}_{agg_col}_{window_size}_{x}' for x in func_trans ]\n",
    "    grp_data = grp_data.reset_index()\n",
    "    return grp_data\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 活期交易表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr_train = pd.read_csv(os.path.join(train_path,'DZ_TR_APS.csv'))\n",
    "tr_test = pd.read_csv(os.path.join(stage_path,f'DZ_TR_APS_{stage}.csv'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fe_tr_aps(df,merge_df,end_date,prefix='aps'):\n",
    "    df = df.rename({'apsdcusno':'CUST_NO'},axis=1)\n",
    "    df['apsdtrdat_d'] = pd.to_datetime(df['apsdtrdat'].astype('str'))\n",
    "    df = df.sort_values(['CUST_NO','apsdtrdat'])\n",
    "    func_trans = ['sum','min','max','mean','std','count']\n",
    "    for win_size in [1,3,5,7,15,30,60]:\n",
    "        df_grp = slide_window_stats(df,'apsdtrdat_d','CUST_NO','apsdtramt',win_size,prefix,end_date,func_trans)\n",
    "        merge_df = merge_df.merge(df_grp,on='CUST_NO',how='left')\n",
    "\n",
    "    return merge_df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = fe_tr_aps(tr_train,df_train_user,end_date_train)\n",
    "tmp_test = fe_tr_aps(tr_test,df_test_user,end_date_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 提取最后日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr_train1 = pd.read_csv(os.path.join(train_path,'DZ_TR_APS.csv'))\n",
    "tr_test1 = pd.read_csv(os.path.join(stage_path,f'DZ_TR_APS_{stage}.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apsdtrdat</th>\n",
       "      <th>apsdcusno</th>\n",
       "      <th>apsdtrcod</th>\n",
       "      <th>apsdtramt</th>\n",
       "      <th>apsdabs</th>\n",
       "      <th>apsdtrchl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>19931113</td>\n",
       "      <td>72df708208980fb86cf665ac112a2b83</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-5.335125</td>\n",
       "      <td>d0beef25bb4c882e0fef596812c2184d</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>19931122</td>\n",
       "      <td>bf79d5f569fdabcf0a95f63840a2b913</td>\n",
       "      <td>cacde09efdb7ac914543e7ae722bbd8c</td>\n",
       "      <td>50.138418</td>\n",
       "      <td>0cd2ec10e9d5e1a149c8223254b55b93</td>\n",
       "      <td>489acf879109967adb41b12dc8c24a29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19931129</td>\n",
       "      <td>8181d3a4bfab65da81af8aaa08944c6e</td>\n",
       "      <td>7df22ab6cbcf897e7dedcd15562801ab</td>\n",
       "      <td>10.652436</td>\n",
       "      <td>d0beef25bb4c882e0fef596812c2184d</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19931009</td>\n",
       "      <td>2f94df1c36323ded35707bed2ef5a44c</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-7.784160</td>\n",
       "      <td>2657656fee8f58d9d418a3539a951e9e</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>19931011</td>\n",
       "      <td>09bee1ce8223e11ae30af87b23068ce0</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-22.988357</td>\n",
       "      <td>2ac97d3b7aea888b7ca7c2b4a85e5c1d</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   apsdtrdat                         apsdcusno  \\\n",
       "0   19931113  72df708208980fb86cf665ac112a2b83   \n",
       "1   19931122  bf79d5f569fdabcf0a95f63840a2b913   \n",
       "2   19931129  8181d3a4bfab65da81af8aaa08944c6e   \n",
       "3   19931009  2f94df1c36323ded35707bed2ef5a44c   \n",
       "4   19931011  09bee1ce8223e11ae30af87b23068ce0   \n",
       "\n",
       "                          apsdtrcod  apsdtramt  \\\n",
       "0  98e06f27adaac9f0189bfe26919bd52d  -5.335125   \n",
       "1  cacde09efdb7ac914543e7ae722bbd8c  50.138418   \n",
       "2  7df22ab6cbcf897e7dedcd15562801ab  10.652436   \n",
       "3  98e06f27adaac9f0189bfe26919bd52d  -7.784160   \n",
       "4  98e06f27adaac9f0189bfe26919bd52d -22.988357   \n",
       "\n",
       "                            apsdabs                         apsdtrchl  \n",
       "0  d0beef25bb4c882e0fef596812c2184d  a4aef37cfb415c1625d392f379185e30  \n",
       "1  0cd2ec10e9d5e1a149c8223254b55b93  489acf879109967adb41b12dc8c24a29  \n",
       "2  d0beef25bb4c882e0fef596812c2184d  a4aef37cfb415c1625d392f379185e30  \n",
       "3  2657656fee8f58d9d418a3539a951e9e  a4aef37cfb415c1625d392f379185e30  \n",
       "4  2ac97d3b7aea888b7ca7c2b4a85e5c1d  a4aef37cfb415c1625d392f379185e30  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tr_train1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def date_diff(df):\n",
    "    df['date_new'] = pd.to_datetime(df['apsdtrdat'],format = '%Y%m%d')\n",
    "    df2 = df.drop_duplicates(subset=['apsdtrdat','apsdcusno'],keep='first')\n",
    "    df2['date_diff'] = df2.sort_values('apsdtrdat').groupby('apsdcusno')['date_new'].diff().dt.days\n",
    "    df2 = df2.sort_values('apsdtrdat').dropna()\n",
    "    return df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-34-af193fef2239>:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df2['date_diff'] = df2.sort_values('apsdtrdat').groupby('apsdcusno')['date_new'].diff().dt.days\n"
     ]
    }
   ],
   "source": [
    "tr_train2 = date_diff(tr_train1)\n",
    "tr_test2 = date_diff(tr_test1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apsdtrdat</th>\n",
       "      <th>apsdcusno</th>\n",
       "      <th>apsdtrcod</th>\n",
       "      <th>apsdtramt</th>\n",
       "      <th>apsdabs</th>\n",
       "      <th>apsdtrchl</th>\n",
       "      <th>date_new</th>\n",
       "      <th>date_diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>91593</th>\n",
       "      <td>19931001</td>\n",
       "      <td>b3fbf023ed153ad4e39df730684983d6</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-11.494178</td>\n",
       "      <td>2657656fee8f58d9d418a3539a951e9e</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "      <td>1993-10-01</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137455</th>\n",
       "      <td>19931001</td>\n",
       "      <td>b72d473acae11a66564838987205e645</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-7.207903</td>\n",
       "      <td>2657656fee8f58d9d418a3539a951e9e</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "      <td>1993-10-01</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996446</th>\n",
       "      <td>19931001</td>\n",
       "      <td>9596b279d9b8f97408cf6c8e6170d853</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-7.519644</td>\n",
       "      <td>2ac97d3b7aea888b7ca7c2b4a85e5c1d</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "      <td>1993-10-01</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>608628</th>\n",
       "      <td>19931001</td>\n",
       "      <td>5b50ff140a2d0132468dc1edea88a9e8</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-4.499819</td>\n",
       "      <td>d0beef25bb4c882e0fef596812c2184d</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "      <td>1993-10-01</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50327</th>\n",
       "      <td>19931001</td>\n",
       "      <td>4123478e1902afd2f44ebd2b2204e5bf</td>\n",
       "      <td>98e06f27adaac9f0189bfe26919bd52d</td>\n",
       "      <td>-14.254216</td>\n",
       "      <td>2657656fee8f58d9d418a3539a951e9e</td>\n",
       "      <td>a4aef37cfb415c1625d392f379185e30</td>\n",
       "      <td>1993-10-01</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        apsdtrdat                         apsdcusno  \\\n",
       "91593    19931001  b3fbf023ed153ad4e39df730684983d6   \n",
       "137455   19931001  b72d473acae11a66564838987205e645   \n",
       "996446   19931001  9596b279d9b8f97408cf6c8e6170d853   \n",
       "608628   19931001  5b50ff140a2d0132468dc1edea88a9e8   \n",
       "50327    19931001  4123478e1902afd2f44ebd2b2204e5bf   \n",
       "\n",
       "                               apsdtrcod  apsdtramt  \\\n",
       "91593   98e06f27adaac9f0189bfe26919bd52d -11.494178   \n",
       "137455  98e06f27adaac9f0189bfe26919bd52d  -7.207903   \n",
       "996446  98e06f27adaac9f0189bfe26919bd52d  -7.519644   \n",
       "608628  98e06f27adaac9f0189bfe26919bd52d  -4.499819   \n",
       "50327   98e06f27adaac9f0189bfe26919bd52d -14.254216   \n",
       "\n",
       "                                 apsdabs                         apsdtrchl  \\\n",
       "91593   2657656fee8f58d9d418a3539a951e9e  a4aef37cfb415c1625d392f379185e30   \n",
       "137455  2657656fee8f58d9d418a3539a951e9e  a4aef37cfb415c1625d392f379185e30   \n",
       "996446  2ac97d3b7aea888b7ca7c2b4a85e5c1d  a4aef37cfb415c1625d392f379185e30   \n",
       "608628  d0beef25bb4c882e0fef596812c2184d  a4aef37cfb415c1625d392f379185e30   \n",
       "50327   2657656fee8f58d9d418a3539a951e9e  a4aef37cfb415c1625d392f379185e30   \n",
       "\n",
       "         date_new  date_diff  \n",
       "91593  1993-10-01        1.0  \n",
       "137455 1993-10-01        1.0  \n",
       "996446 1993-10-01        1.0  \n",
       "608628 1993-10-01        1.0  \n",
       "50327  1993-10-01        1.0  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tr_train2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0     349922\n",
       "2.0      77217\n",
       "3.0      37417\n",
       "4.0      21894\n",
       "5.0      14817\n",
       "6.0      10509\n",
       "7.0       8237\n",
       "8.0       5831\n",
       "9.0       4498\n",
       "10.0      3642\n",
       "11.0      2962\n",
       "12.0      2471\n",
       "13.0      2182\n",
       "14.0      1915\n",
       "15.0      1584\n",
       "16.0      1336\n",
       "17.0      1189\n",
       "18.0      1076\n",
       "19.0       926\n",
       "20.0       817\n",
       "21.0       764\n",
       "22.0       734\n",
       "31.0       667\n",
       "30.0       618\n",
       "23.0       613\n",
       "29.0       595\n",
       "24.0       587\n",
       "25.0       515\n",
       "28.0       485\n",
       "26.0       472\n",
       "27.0       459\n",
       "32.0       365\n",
       "33.0       263\n",
       "35.0       166\n",
       "34.0       161\n",
       "36.0        99\n",
       "37.0        88\n",
       "38.0        82\n",
       "40.0        79\n",
       "41.0        65\n",
       "39.0        61\n",
       "42.0        61\n",
       "45.0        48\n",
       "46.0        41\n",
       "43.0        38\n",
       "44.0        36\n",
       "50.0        34\n",
       "47.0        34\n",
       "48.0        32\n",
       "49.0        30\n",
       "52.0        19\n",
       "51.0        19\n",
       "55.0        18\n",
       "54.0        17\n",
       "53.0        16\n",
       "57.0        10\n",
       "56.0         5\n",
       "59.0         3\n",
       "58.0         2\n",
       "60.0         1\n",
       "Name: date_diff, dtype: int64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tr_train2.date_diff.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fe_tr_date(df,enddate):\n",
    "    df = df.rename({'apsdcusno':'CUST_NO'},axis=1)\n",
    "    df_grp = df.groupby('CUST_NO').agg(\n",
    "        aps_last_date = ('apsdtrdat','max'),  #最后登录日期\n",
    "     #   aps_interval_max = ('date_diff','max'),  #最大时间间隔\n",
    "     #   aps_interval_min = ('date_diff','min'),\n",
    "     #   aps_interval_mean = ('date_diff','mean'),\n",
    "     #   aps_interval_std = ('date_diff','std'),\n",
    "     #   aps_interval_count = ('date_diff','count')\n",
    "    )\n",
    "  #  df_grp['aps_interval_count'] = df_grp['aps_interval_count'] + 1\n",
    "    df_grp['aps_last_date'] = (enddate - pd.to_datetime(df_grp['aps_last_date'],format = '%Y%m%d')).dt.days\n",
    "    df_grp = df_grp.reset_index()\n",
    "    return df_grp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr_train3 = fe_tr_date(tr_train2,end_date_train)\n",
    "tr_test3 = fe_tr_date(tr_test2,end_date_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = tmp_train.merge(tr_train3,on='CUST_NO',how='left')\n",
    "tmp_test = tmp_test.merge(tr_test3,on='CUST_NO',how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train['aps_last_date'] = tmp_train['aps_last_date'].fillna(999)\n",
    "tmp_test['aps_last_date'] = tmp_test['aps_last_date'].fillna(999)\n",
    "tmp_train = tmp_train.fillna(0)\n",
    "tmp_test = tmp_test.fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CUST_NO</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_1_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_1_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_1_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_1_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_1_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_1_count</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_3_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_3_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_3_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_3_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_3_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_3_count</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_5_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_5_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_5_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_5_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_5_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_5_count</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_7_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_7_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_7_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_7_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_7_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_7_count</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_15_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_15_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_15_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_15_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_15_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_15_count</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_30_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_30_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_30_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_30_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_30_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_30_count</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_60_sum</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_60_min</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_60_max</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_60_mean</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_60_std</th>\n",
       "      <th>aps_CUST_NO_apsdtramt_60_count</th>\n",
       "      <th>aps_last_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>235e4e193124d8c55095cf3f0f0d8f35</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>f1b5ca32a8f7ef5430f5775c00ff3f60</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>51be6f380b408edeb7779b76e016dcd3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2.556282</td>\n",
       "      <td>-2.556282</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.278141</td>\n",
       "      <td>1.807565</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-2.556282</td>\n",
       "      <td>-2.556282</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.278141</td>\n",
       "      <td>1.807565</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-2.556282</td>\n",
       "      <td>-2.556282</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.278141</td>\n",
       "      <td>1.807565</td>\n",
       "      <td>2.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ccd7e33ccbe7e9dd4246a2959f666c0a</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-8.257676</td>\n",
       "      <td>-8.257676</td>\n",
       "      <td>-8.257676</td>\n",
       "      <td>-8.257676</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>28.234065</td>\n",
       "      <td>-8.257676</td>\n",
       "      <td>36.491741</td>\n",
       "      <td>14.117033</td>\n",
       "      <td>31.642616</td>\n",
       "      <td>2.0</td>\n",
       "      <td>19.451852</td>\n",
       "      <td>-83.254131</td>\n",
       "      <td>83.300844</td>\n",
       "      <td>3.890370</td>\n",
       "      <td>61.790761</td>\n",
       "      <td>5.0</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>069f48f51bf6be5bcbdc9af52bb20970</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-123.49848</td>\n",
       "      <td>-21.251208</td>\n",
       "      <td>33.154947</td>\n",
       "      <td>-8.82132</td>\n",
       "      <td>13.228012</td>\n",
       "      <td>14.0</td>\n",
       "      <td>-137.656318</td>\n",
       "      <td>-31.859766</td>\n",
       "      <td>33.154947</td>\n",
       "      <td>-8.09743</td>\n",
       "      <td>16.960602</td>\n",
       "      <td>17.0</td>\n",
       "      <td>-137.656318</td>\n",
       "      <td>-31.859766</td>\n",
       "      <td>33.154947</td>\n",
       "      <td>-8.09743</td>\n",
       "      <td>16.960602</td>\n",
       "      <td>17.0</td>\n",
       "      <td>-174.155486</td>\n",
       "      <td>-42.344915</td>\n",
       "      <td>42.344915</td>\n",
       "      <td>-5.122220</td>\n",
       "      <td>19.844642</td>\n",
       "      <td>34.0</td>\n",
       "      <td>-328.378033</td>\n",
       "      <td>-42.344915</td>\n",
       "      <td>42.344915</td>\n",
       "      <td>-5.130907</td>\n",
       "      <td>17.853094</td>\n",
       "      <td>64.0</td>\n",
       "      <td>-443.849364</td>\n",
       "      <td>-42.344915</td>\n",
       "      <td>42.344915</td>\n",
       "      <td>-4.187258</td>\n",
       "      <td>18.364779</td>\n",
       "      <td>106.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            CUST_NO  aps_CUST_NO_apsdtramt_1_sum  \\\n",
       "0  235e4e193124d8c55095cf3f0f0d8f35                          0.0   \n",
       "1  f1b5ca32a8f7ef5430f5775c00ff3f60                          0.0   \n",
       "2  51be6f380b408edeb7779b76e016dcd3                          0.0   \n",
       "3  ccd7e33ccbe7e9dd4246a2959f666c0a                          0.0   \n",
       "4  069f48f51bf6be5bcbdc9af52bb20970                          0.0   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_1_min  aps_CUST_NO_apsdtramt_1_max  \\\n",
       "0                          0.0                          0.0   \n",
       "1                          0.0                          0.0   \n",
       "2                          0.0                          0.0   \n",
       "3                          0.0                          0.0   \n",
       "4                          0.0                          0.0   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_1_mean  aps_CUST_NO_apsdtramt_1_std  \\\n",
       "0                           0.0                          0.0   \n",
       "1                           0.0                          0.0   \n",
       "2                           0.0                          0.0   \n",
       "3                           0.0                          0.0   \n",
       "4                           0.0                          0.0   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_1_count  aps_CUST_NO_apsdtramt_3_sum  \\\n",
       "0                            0.0                      0.00000   \n",
       "1                            0.0                      0.00000   \n",
       "2                            0.0                      0.00000   \n",
       "3                            0.0                      0.00000   \n",
       "4                            0.0                   -123.49848   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_3_min  aps_CUST_NO_apsdtramt_3_max  \\\n",
       "0                     0.000000                     0.000000   \n",
       "1                     0.000000                     0.000000   \n",
       "2                     0.000000                     0.000000   \n",
       "3                     0.000000                     0.000000   \n",
       "4                   -21.251208                    33.154947   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_3_mean  aps_CUST_NO_apsdtramt_3_std  \\\n",
       "0                       0.00000                     0.000000   \n",
       "1                       0.00000                     0.000000   \n",
       "2                       0.00000                     0.000000   \n",
       "3                       0.00000                     0.000000   \n",
       "4                      -8.82132                    13.228012   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_3_count  aps_CUST_NO_apsdtramt_5_sum  \\\n",
       "0                            0.0                     0.000000   \n",
       "1                            0.0                     0.000000   \n",
       "2                            0.0                     0.000000   \n",
       "3                            0.0                     0.000000   \n",
       "4                           14.0                  -137.656318   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_5_min  aps_CUST_NO_apsdtramt_5_max  \\\n",
       "0                     0.000000                     0.000000   \n",
       "1                     0.000000                     0.000000   \n",
       "2                     0.000000                     0.000000   \n",
       "3                     0.000000                     0.000000   \n",
       "4                   -31.859766                    33.154947   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_5_mean  aps_CUST_NO_apsdtramt_5_std  \\\n",
       "0                       0.00000                     0.000000   \n",
       "1                       0.00000                     0.000000   \n",
       "2                       0.00000                     0.000000   \n",
       "3                       0.00000                     0.000000   \n",
       "4                      -8.09743                    16.960602   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_5_count  aps_CUST_NO_apsdtramt_7_sum  \\\n",
       "0                            0.0                     0.000000   \n",
       "1                            0.0                     0.000000   \n",
       "2                            0.0                     0.000000   \n",
       "3                            0.0                     0.000000   \n",
       "4                           17.0                  -137.656318   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_7_min  aps_CUST_NO_apsdtramt_7_max  \\\n",
       "0                     0.000000                     0.000000   \n",
       "1                     0.000000                     0.000000   \n",
       "2                     0.000000                     0.000000   \n",
       "3                     0.000000                     0.000000   \n",
       "4                   -31.859766                    33.154947   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_7_mean  aps_CUST_NO_apsdtramt_7_std  \\\n",
       "0                       0.00000                     0.000000   \n",
       "1                       0.00000                     0.000000   \n",
       "2                       0.00000                     0.000000   \n",
       "3                       0.00000                     0.000000   \n",
       "4                      -8.09743                    16.960602   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_7_count  aps_CUST_NO_apsdtramt_15_sum  \\\n",
       "0                            0.0                      0.000000   \n",
       "1                            0.0                      0.000000   \n",
       "2                            0.0                     -2.556282   \n",
       "3                            0.0                     -8.257676   \n",
       "4                           17.0                   -174.155486   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_15_min  aps_CUST_NO_apsdtramt_15_max  \\\n",
       "0                      0.000000                      0.000000   \n",
       "1                      0.000000                      0.000000   \n",
       "2                     -2.556282                      0.000000   \n",
       "3                     -8.257676                     -8.257676   \n",
       "4                    -42.344915                     42.344915   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_15_mean  aps_CUST_NO_apsdtramt_15_std  \\\n",
       "0                       0.000000                      0.000000   \n",
       "1                       0.000000                      0.000000   \n",
       "2                      -1.278141                      1.807565   \n",
       "3                      -8.257676                      0.000000   \n",
       "4                      -5.122220                     19.844642   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_15_count  aps_CUST_NO_apsdtramt_30_sum  \\\n",
       "0                             0.0                      0.000000   \n",
       "1                             0.0                      0.000000   \n",
       "2                             2.0                     -2.556282   \n",
       "3                             1.0                     28.234065   \n",
       "4                            34.0                   -328.378033   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_30_min  aps_CUST_NO_apsdtramt_30_max  \\\n",
       "0                      0.000000                      0.000000   \n",
       "1                      0.000000                      0.000000   \n",
       "2                     -2.556282                      0.000000   \n",
       "3                     -8.257676                     36.491741   \n",
       "4                    -42.344915                     42.344915   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_30_mean  aps_CUST_NO_apsdtramt_30_std  \\\n",
       "0                       0.000000                      0.000000   \n",
       "1                       0.000000                      0.000000   \n",
       "2                      -1.278141                      1.807565   \n",
       "3                      14.117033                     31.642616   \n",
       "4                      -5.130907                     17.853094   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_30_count  aps_CUST_NO_apsdtramt_60_sum  \\\n",
       "0                             0.0                      0.000000   \n",
       "1                             0.0                      0.000000   \n",
       "2                             2.0                     -2.556282   \n",
       "3                             2.0                     19.451852   \n",
       "4                            64.0                   -443.849364   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_60_min  aps_CUST_NO_apsdtramt_60_max  \\\n",
       "0                      0.000000                      0.000000   \n",
       "1                      0.000000                      0.000000   \n",
       "2                     -2.556282                      0.000000   \n",
       "3                    -83.254131                     83.300844   \n",
       "4                    -42.344915                     42.344915   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_60_mean  aps_CUST_NO_apsdtramt_60_std  \\\n",
       "0                       0.000000                      0.000000   \n",
       "1                       0.000000                      0.000000   \n",
       "2                      -1.278141                      1.807565   \n",
       "3                       3.890370                     61.790761   \n",
       "4                      -4.187258                     18.364779   \n",
       "\n",
       "   aps_CUST_NO_apsdtramt_60_count  aps_last_date  \n",
       "0                             0.0          999.0  \n",
       "1                             0.0          999.0  \n",
       "2                             2.0          999.0  \n",
       "3                             5.0           14.0  \n",
       "4                           106.0            1.0  "
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.set_option('display.max_columns',None)\n",
    "tmp_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train.to_csv('../fea/train_tr_aps.csv',index=False)\n",
    "tmp_test.to_csv('../fea/test_tr_aps.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
